package com.wuzihao.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuzihao.reggie.common.R;
import com.wuzihao.reggie.entity.Employee;
import com.wuzihao.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> employeeLogin(HttpServletRequest request ,@RequestBody Employee employee){

        R result = employeeService.login(request,employee);
        Employee data = (Employee) result.getData();
        return R.success(data);
    }

    @PostMapping("/logout")
    public R employeeLogout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出登录");
    }

    @PostMapping
    public R save(HttpServletRequest request,@RequestBody Employee employee){
        employeeService.saveEmployee(request,employee);

        return R.success("新增员工成功");
    }


    @GetMapping("/page")
    // TODO page,pageSize,name可以直接获取到
    public R page(int page,int pageSize,String name){
        Page pageInfo = employeeService.pageInfo(page,pageSize,name);
        return R.success(pageInfo);
    }

    @PutMapping
    public R update(HttpServletRequest request,@RequestBody Employee employee){
        employeeService.updateStatus(request,employee);

        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee == null){
            return R.error("员工信息查询失败");
        }
        return R.success("员工信息修改成功");
    }
}
